﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PearTunes.Client.PearTunesServiceReference;

namespace PearTunes.Client
{
    /// <summary>
    /// Interaction logic for Store.xaml
    /// </summary>
    public partial class StorePage : Page
    {
        

        public StorePage()
        {
            InitializeComponent();
        }

        public string SelectedFilePath
        {
            get
            {
                return (musicListView.SelectedItem as Track).FilePath;
            }
        }

        private void searchButton_Click(object sender, RoutedEventArgs e)
        {
            PearTunesServiceReference.PearTunesServiceClient proxy = new PearTunesServiceReference.PearTunesServiceClient();
            proxy.Open();
            musicListView.Items.Clear();
            string what = searchTextBox.Text;
            var tracks = from track in proxy.GetTracks()
                         where track.Title.ToLower().Contains(what) == true ||
                               track.Artist.ToLower().Contains(what) == true
                         orderby track.Artist ascending
                         select track;
            foreach (var item in tracks)
            {
                musicListView.Items.Add(item);
            }
            proxy.Close();
        }

        private void buyButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.SaveToMyCollection(musicListView.SelectedItem as PearTunesServiceReference.Track);
        }

        private void listenButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.PlayMp3FromUrl((musicListView.SelectedItem as PearTunesServiceReference.Track).FilePath);
        }
    }
}
